/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.power.powercash.report;

import com.power.utils.PowerImage;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.java.dev.transparentlayout.TransparentLayout;

/**
 *
 * @author f6036477
 */
public class HorizontalResizePanel extends JPanel implements MouseListener, MouseMotionListener {

	public static final int DEFAULT_LINE_WIDTH = 1;

	public static final int DEFAULT_DRAG_SIZE_AREA = 7;

	public static final int DEFAULT_MIN_WIDTH = 30;

	public static final Color
			DEFAULT_COLOR = Color.BLACK,
			DEFAULT_DRAG_COLOR = new Color(110, 110, 255);


	private TransparentLayout layout;

	private int lineWidth;

	private int dragSizeArea;

	private int minsize;

	private Color color, lineColor, dragColor;

	private Point lastClick;

	private boolean dragging, exited;

	private int totaldif;


	public HorizontalResizePanel() {
		super();
		color = DEFAULT_COLOR;
		lineColor = color;
		dragColor = DEFAULT_DRAG_COLOR;
		lineWidth = DEFAULT_LINE_WIDTH;
		dragSizeArea = DEFAULT_DRAG_SIZE_AREA;
		minsize = DEFAULT_MIN_WIDTH;
		dragging = false;
		exited = true;
		totaldif = -1;
		layout = new TransparentLayout();
		this.setLayout(layout);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}


	@Override
	protected void paintComponent(Graphics gp) {
		super.paintComponent(gp);
		Graphics2D g = (Graphics2D) gp.create();
		g.setColor(lineColor);
		g.setStroke(new BasicStroke(1.6f));
		int x = this.getSize().width - (lineWidth +1);
		g.drawLine(x, 0, x, this.getSize().height);
	}


	public static Rectangle toRect(int x, int y, int w, int h) {
		return new Rectangle(x, y, w, h);
	}


	@Override
	public void setPreferredSize(Dimension d) {
		super.setPreferredSize(d);
		super.setSize(d);
	}


	@Override
	public void mouseClicked(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {
	}
	@Override
	public void mouseExited(MouseEvent e) {
		if(e.getLocationOnScreen().x < this.getLocationOnScreen().x)
			exited = true;
		if(!dragging) {
			lineColor = color;
			this.repaint();
		}
	}


	@Override
	public void mousePressed(MouseEvent e) {
		if(e.getX() >= this.getSize().width - dragSizeArea)
			lastClick = e.getLocationOnScreen();
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		lastClick = null;
		if(exited) {
			dragging = false;
			lineColor = color;
		}
		this.repaint();
		if(totaldif != -1) {
			this.firePropertyChange("size", this, totaldif);
			totaldif = -1;
		}
	}


	@Override
	public void mouseDragged(MouseEvent e) {
		if(e.getX() >=
				(this.getSize().width - dragSizeArea))
			exited = false;

		if(lastClick != null) {
			dragging = true;
			int dif = e.getXOnScreen() - lastClick.x;
			lastClick = e.getLocationOnScreen();

			if((dif < 0 && this.getSize().width <= minsize)
					|| exited) return;

			totaldif += dif;
			this.setPreferredSize(
					new Dimension(this.getWidth() + dif, this.getHeight()));
		}
	}

	
	@Override
	public void mouseMoved(MouseEvent e) {
		if(e.getX() >=
				(this.getSize().width - dragSizeArea))
			exited = false;

		if(e.getX() >= this.getSize().width - dragSizeArea) {
			lineColor = dragColor;
		} else {
			lineColor = color;
		}
		this.repaint();
	}


	public Color getLineColor() {
		return color;
	}


	public void setLineColor(Color color) {
		this.color = color;
	}


	public Color getLineDragColor() {
		return dragColor;
	}


	public void setLineDragColor(Color dragColor) {
		this.dragColor = dragColor;
	}


	public int getDragSizeArea() {
		return dragSizeArea;
	}


	public void setDragSizeArea(int dragSizeArea) {
		this.dragSizeArea = dragSizeArea;
	}


	public int getLineWidth() {
		return lineWidth;
	}


	public void setLineWidth(int lineWidth) {
		this.lineWidth = lineWidth;
	}


	public int getMinimumWidth() {
		return minsize;
	}


	public void setMinimumWidth(int minsize) {
		this.minsize = minsize;
	}


	public static void main(String[] args) {
		JFrame f = new JFrame("Test");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		HorizontalResizePanel hp = new HorizontalResizePanel();
		hp.setSize(100, 100);
		hp.setBorder(BorderFactory.createLineBorder(Color.green));
		hp.setBounds(10, 10, 100, 100);

		HorizontalResizePanel hq = new HorizontalResizePanel();
		hq.setSize(100, 100);
		hq.setBorder(BorderFactory.createLineBorder(Color.green));
		hq.setBounds(111, 10, 100, 100);

		HorizontalResizePanel hr = new HorizontalResizePanel();
		hr.setSize(100, 100);
		hr.setBorder(BorderFactory.createLineBorder(Color.green));
		hr.setBounds(212, 10, 100, 100);

		HorizontalResizeManager hm = new HorizontalResizeManager();
		hm.add(hp);
		hm.add(hq);
		hm.add(hr);

		f.setLayout(null);
		f.add(hp);
		f.add(hq);
		f.add(hr);
		f.setSize(340, 160);
		f.setVisible(true);

	}

}
